// pages/offline/offline.js
const app = getApp()
var msgList = []
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		dataInfo: {},
		imgBaseUrl: app.globalData.baseImgUrl,
		showModal: false,
		timeout: 10,
		isinure: false
	},

	// 回复文字文本框失去焦点时获取内容
	getReplyContent: function (e) {
		console.log(e.detail.value)
		this.data.dataInfo.user_info.auto_reply_text = e.detail.value
		this.setData({
			dataInfo: this.data.dataInfo
		})
	},

	// 点击上传离线图片
	uploadReplyImg: function () {
		var that = this
		wx.chooseImage({
			success(res) {
				const tempFilePaths = res.tempFilePaths
				console.log(tempFilePaths)
				wx.uploadFile({
          url: app.globalData.baseUrl + '/api/csuser/uploadpic', //仅为示例，非真实的接口地址
					filePath: tempFilePaths[0],
					name: 'image',
					success(res) {
						console.log(res)
						const data = JSON.parse(res.data)
						if (data.error === 0) {
							console.log(that.data.dataInfo)
							that.data.dataInfo.user_info.auto_reply_img = data.url
							that.setData({
								dataInfo: that.data.dataInfo
							})
							console.log(that.dataInfo)
						}
						//do something
					}
				})
			}
		})
	},

	// 点击保存离线回复
	saveOfflineReply: function () {
		var that = this
		wx.request({
			url: app.globalData.baseUrl + '/api/csuser/savereply',
			method: 'POST',
			data: {
				auto_reply_img: that.data.dataInfo.user_info.auto_reply_img,
				auto_reply_text: that.data.dataInfo.user_info.auto_reply_text,
				id: that.data.dataInfo.user_info.id
			},
			success: function (res) {
				console.log(res)
				if (res.data.status === 1) {
					wx.showToast({
						title: '设置成功',
						icon: 'success',
						duration: 1000
					})
				}
				wx.setStorage({
					key: "islogin",
					data: that.data.dataInfo
				})
			}
		})
	},
	// 点击设置超时自动回复按钮
	clickOvertimeBtn: function () {
		var that = this
		wx.request({
			url: app.globalData.baseUrl +  '/api/gettimeout',
			method: 'POST',
			data: {
				cs_id: this.data.dataInfo.user_info.id
			},
			success: function (res) {
				console.log(res)
				that.setData({
					isinure: res.data.arr.isinure,
					timeout: res.data.arr.timeout/1000
				})
				that.setData({
					showModal: true
				})
			}
		})
	},

	// 超时时间改变时触发
	timeOverInputChange: function (e) {
		console.log(e.detail.value)
		this.setData({
			timeout: e.detail.value
		})
	},

	// 点击超时开关时触发事件
	switchChange: function (e) {
		this.setData({
			isinure: e.detail.value
		})
	},
	// 点击隐藏弹窗
	clickHideModal: function () {
		this.setData({
			showModal: false
		})
	},

	// 点击保存超时自动回复
	clickSaveOverTimeReply: function () {
		var that = this
		wx.request({
			url: app.globalData.baseUrl + '/api/settimeout',
			method: 'POST',
			data: {
				cs_id: this.data.dataInfo.user_info.id,
				isinure: this.data.isinure,
				timeout: this.data.timeout * 1000
			},
			success: function (res) {
				console.log(res)
				if (res.data.status === 1) {
					wx.showToast({
						title: '设置成功',
						icon: 'success',
						duration: 1000
					})

					that.setData({
						showModal: false
					})
				}
			}
		})
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		wx.setNavigationBarTitle({
			title: '离线回复设置'
		})

		// 获取缓存里面的客服信息
		try {
			var value = wx.getStorageSync('islogin')
			if (value) {
				console.log(value)
				// 如果缓存里面有登录信息则发送请求
				this.setData({
					dataInfo: value
				})
				console.log()
			} else {
				// wx.navigateTo({
				// 	url: '../login/login'
				// })
			}
		} catch (e) {
			console.log(e)
		}

	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
		var that = this;
		app.sureConnect()
		wx.onSocketOpen(function (res) {
			that.onopen(res)
		})

		wx.onSocketMessage(function (res) {
			that.onmessage(res)
		})
	},

	onopen: function (res) {
		console.log(res)
	},

	onmessage: function (res) {
		var that = this
		// console.log(res)
		var data = JSON.parse(res.data)
		if (data.event === 're_connect') {
			app.heartbeat()
		} else if (data.event === 'heartbeat_new') {
			app.heartbeat()
		} else if (data.event === 'sendMsg') {
			console.log(data)
			msgList.push(data.data)
		}
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {
		wx.setStorage({
			key: "newMsgList",
			data: msgList
		})
		msgList = []
	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})